<?php
include_once("round.php");
if(class_exists('SQL_Helper') != true)
{
   include_once("sql_helper.php");
}
class RoundRepository
{

function createRound($seasonID, $round_number,$round_date){
	SQL_Helper::executeNonQuery("INSERT INTO `rounds` (`roundID`, `seasonID`, `round_number`, `round_date`) VALUES(null, ?, ?, ?)",$seasonID, $round_number,$round_date);
}

function readRound($roundID){
	return SQL_Helper::executeQuerySingle("Select * from `rounds` where `roundID` = ?", $roundID);
}

function updateRound($roundID, $seasonID, $round_number,$round_date){
	SQL_Helper::executeNonQuery("Update `rounds` set `seasonID`=?, `round_number`=?, `round_date`=? where `roundID` = ?",$seasonID, $round_number,$round_date,$roundID);
}

function deleteRound($roundID){
	SQL_Helper::executeNonQuery("delete from `rounds` where `roundID` = ?", $roundID);
}

function map($row){
	$class= "Round";
	return SQL_Helper::sql_map($row,$class);
}

function maps($rows){
		$class= "Round";
		return SQL_Helper::sql_maps($rows,$class);
}
	
function getRoundByDate ($date){
	$row = SQL_Helper::executeQuerySingle("SELECT `roundID` FROM  `rounds` WHERE  `round_date` < curdate() ORDER BY `round_date` DESC LIMIT 0 , 1");
	return $row['roundID'];
}

function getFirstRound($seasonID){	
	$row = SQL_Helper::executeQuerySingle("SELECT roundID FROM  `rounds` WHERE `round_number` = (SELECT MIN(`round_number`) FROM `rounds` WHERE  `seasonID` =  ? )", $seasonID);
	return $row['roundID'];
}

function getAllRounds($seasonID){	
	return SQL_Helper::executeQuery("SELECT * FROM  `rounds` WHERE `seasonID` =  ? ", $seasonID); 
}
function getNextRoundByDate ($date){
	$row = SQL_Helper::executeQuerySingle("SELECT `roundID` FROM  `rounds` WHERE  `round_date` >= curdate() ORDER BY `round_date` ASC LIMIT 0 , 1");
	return $row['roundID'];
}

function getAllRoundsDESC($seasonID){	
	return SQL_Helper::executeQuery("SELECT * FROM  `rounds` WHERE `seasonID` =  ? order by `round_date` DESC", $seasonID); 
}

function getAllRoundsASC($seasonID){	
	return SQL_Helper::executeQuery("SELECT * FROM  `rounds` WHERE `seasonID` =  ? order by `round_date` ASC", $seasonID); 
}
}
?>